package exercise;

import java.io.*;
import java.util.Scanner;

/**
 * 进行普通文件的复制
 * 把 inputStream 中的数据读出来, 写入到 outputStream 中
 */

public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件:>");
        String rootFilePath = scanner.nextLine();
        System.out.println("请输入目标文件:>");
        String toFilePath = scanner.nextLine();
        File rootFile = new File(rootFilePath);
        if (!rootFile.isFile()){
            System.out.println("输入路径不正确");
            return ;
        }
        try(InputStream inputStream = new FileInputStream(rootFile)){
            try(OutputStream outputStream = new FileOutputStream(toFilePath)){
                //先读
                byte[] bytes = new byte[1024];
                while (true){
                    int len = inputStream.read(bytes);
                    if (len == -1){
                        break;
                    }
                    //只把len长度的数据放入数组
                    outputStream.write(bytes, 0, len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
